import session from './session'
import storage from './storage'
import system from './system'

const config = {
    env: /\/sky-forest\//.test(location.href) ? 'prod' : 'prod',
    appId: '102',
    isWxReady:false,
    weiXinDebug(){
        //return this.env != 'prod';
        return false;
    },

    isLogin(){
        return !!this.getToken();
    },

    getToken(){
        let token = session.get('user', 'token');
        if (token) {
            return token;
        }
        token = storage.get('user', 'token');
        if (token) {
            return token;
        }

        token = system.getNativeToken();
        if(system.isIosApp()){
            token = window.token;
        }
        if (token) {
            this.setToken(token);
            return token;
        }

        return null;
    },
    getUuid(){
        
        let uuid = session.get('user', 'uuid');
        if (uuid) {
            return uuid;
        }
        uuid = storage.get('user', 'uuid');
        if (uuid) {
            return uuid;
        }

        uuid = system.getNativeUuid();
        if(system.isIosApp()){
            uuid = window.uuid;
        }
        if (uuid) {
            this.setUuid(uuid);
            return uuid;
        }

        return null;
    },
    getSessionid(){
 
        let Sessionid = session.get('sky-forest-ubt', 'sessionid');
        //console.log("getSessionid:Session:"+Sessionid);
        if (Sessionid) {
            return Sessionid;
        }
        Sessionid = storage.get('sky-forest-ubt', 'sessionid');
        //console.log("getSessionid:storage:"+Sessionid);
        if (Sessionid) {
            return Sessionid;
        }

        Sessionid = system.getSessionId();
        
        if(system.isIosApp()){
            //console.log("getSessionid:window.Sessionid:"+window.Sessionid);
            Sessionid = window.Sessionid;
        }
        if (Sessionid) {
            this.setSessionid(Sessionid);
            return Sessionid;
        }

        return null;
    },
    getVersion(){
        
        let version = session.get('sky-forest-ubt', 'version');
        //console.log("getversion:version:"+version);
        if (version) {
            return version;
        }
        version = storage.get('sky-forest-ubt', 'version');
        //console.log("getversion:storage:"+version);
        if (version) {
            return version;
        }

        version = system.getVersion();
        
        if(system.isIosApp()){
           // console.log("getversion:window.version:"+window.myVersion);
            version = window.myVersion;
        }
        if (version) {
            this.setVersion(version);
            return version;
        }

        return null;
    },
    removeUser(){
        session.remove('user');
        storage.remove('user');
    },
    setToken(val){
        session.set('user', 'token', val);
        storage.set('user', 'token', val);
    },
    setUuid(val){
        session.set('sky-forest-ubt', 'uuid', val);
        storage.set('sky-forest-ubt', 'uuid', val);
    },
    setUserid(val){
        session.set('user', 'Userid', val);
        storage.set('user', 'Userid', val);
    },
    setSessionid(val){
        session.set('sky-forest-ubt', 'sessionid', val);
        storage.set('sky-forest-ubt', 'sessionid', val);
    },
    setVersion(val){
        session.set('sky-forest-ubt', 'version', val);
        storage.set('sky-forest-ubt', 'version', val);
    },
    setUuid2(val){
        session.set('user', 'uuid', val);
        storage.set('user', 'uuid', val);
    },
    setSessionid2(val){
        session.set('user', 'sessionid', val);
        storage.set('user', 'sessionid', val);
    },
    setPhone(val){
        session.set('user', 'phone', val);
        storage.set('user', 'phone', val);
    },
    setNickname(val){
        session.set('user', 'nickname', val);
        storage.set('user', 'nickname', val);
    },
    getNickname(){
        return session.get('user', 'nickname') || storage.get('user', 'nickname');
    },
    getPhone(){
        return session.get('user', 'phone') || storage.get('user', 'phone') ||system.getMobile();
    },
    hasToken(){
        const token = session.get('user', 'token') || storage.get('user', 'token');
        return !!token;
    },
    getFactoryServiceUrl(path){
        return this._getFactoryBaseUrl() + path;
    },
    getSkyForestUrl(path){
        return this._getSkyForestBaseUrl() + path;
    },
    getNanbinUrl(path){
        return this._getNanbinUrl() + path;
    },
    setStaff(model){
        session.setModel('staff', model)
    },
    getStaffId(){
        return session.get('staff', 'id');
    },
    getImgtUrl(){
        const env = this.env;
        if (env == 'dev') {
            return 'static/';
        }
        if (env == 'test') {
            return 'http://skyforest.static.elab-plus.com/';
        }
        if (env == 'fat') {
            return 'http://skyforest.static.elab-plus.com/';
        }
        if (env == 'uat') {
            return 'http://skyforest.static.elab-plus.com/';
        }
        if (env == 'dev_lhx') {
            return 'http://skyforest.static.elab-plus.com/'
        }
        if(env=='prod1'){
            return 'http://skyforest.static.elab-plus.com/'
        }
        if (env == 'prod') {
            return 'http://skyforest.static.elab-plus.com/';
        }
    },
    getShareUrl(){
        const env = this.env;
        if (env == 'dev') {
            return 'http://yun-image.elab-plus.com/openinstall/';
        }
        if (env == 'test') {
            return 'http://op04hx8n8.bkt.clouddn.com/openinstall/';
        }
        if (env == 'prod') {
            return 'http://yun-image.elab-plus.com/openinstall/';
        }else{
            return 'http://yun-image.elab-plus.com/openinstall/';
        }
    },
    _getFactoryBaseUrl(){
        const env = this.env;
        if (env == 'dev') {
            return 'http://192.168.0.14:9006';
        }
        if (env == 'test') {
            // return 'http://139.196.5.59:9006';
            if(location.href.indexOf('https')==0){
                return 'https://test-factory.elab-plus.com';
            }
            else{
                return 'http://test-factory.elab-plus.com';
            }
        }
        if (env == 'fat') {
            return 'http://139.196.5.59:9006';
        }
        if (env == 'uat') {
            return 'http://139.196.5.82:9006';
        }
        if (env == 'dev_lhx') {
            return 'http://192.168.4.249:9006'
        }
        if(env=='prod1'){
            return 'http://139.196.6.132:9006'
        }
        if (env == 'prod') {
            // 如果页面链接是https协议，则使用https接口
            if(location.href.indexOf('https')==0){
                return 'https://factory.elab-plus.com';
            }
            else{
                return 'http://factory.elab-plus.com';
            }
            
        }
    },
     _getNanbinUrl(){
        const env = this.env;

        if (env == 'test') {
            return 'http://h5test.elab-plus.com/';
        }

        if (env == 'uat') {
            return 'http://139.196.5.82:3002/';
        }
       
        if (env == 'prod') {
            if(location.href.indexOf('https')==0){
                return 'https://m.elab-plus.com/';
            }
            else{
                return 'http://m.elab-plus.com/';
            }
            
        }

        return 'http://m.elab-plus.com/';
    },
    _getSkyForestBaseUrl(){
        const env = this.env;
        if (env == 'dev') {
            return 'http://192.168.0.14:9005';
        }
        if (env == 'test') {
            return 'http://139.196.5.59:9005';
        }
        if (env == 'fat') {
            return 'http://139.196.5.59:9005';
        }
        if (env == 'uat') {
            return 'http://139.196.5.82:9005';
        }
        if (env == 'dev_lhx') {
            return 'http://192.168.4.249:9005'
        }
        if(env=='prod1'){
            return 'http://139.196.6.132:9005'
        }
        if (env == 'prod') {
            if(location.href.indexOf('https')==0){
                return 'https://skyforest.elab-plus.com';
            }
            else{
                return 'http://skyforest.elab-plus.com';
            }
            
        }
    }
};

export default config;
